package tcu.iot.gasmanage.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tcu.iot.gasmanage.common.utils.DateUtils;
import tcu.iot.gasmanage.common.Result;
import tcu.iot.gasmanage.entity.datatable.DataTableQuery;
import tcu.iot.gasmanage.entity.equipment.Equipment;
import tcu.iot.gasmanage.entity.equipment.EquipmentInfoResult;
import tcu.iot.gasmanage.mapper.DataTableMapper;
import tcu.iot.gasmanage.mapper.PersonMapper;
import tcu.iot.gasmanage.mapper.TDengineMapper;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class DataTableService {

    @Resource
    private DataTableMapper dataTableMapper;
    @Resource
    private PersonMapper personMapper;

    //返回所有设备信息
    public Result GetEquipmentInfo(DataTableQuery dataTableQuery) {
        HashMap<String, Object> hashMap = new HashMap<>();
        List<EquipmentInfoResult> list;
        int total;
        String equipmentNumber = dataTableQuery.getEquipmentNumber();

        if (dataTableQuery.getDate() != null) {
            List<String> date = dataTableQuery.getDate();
            String startDate = DateUtils.dealDateFormat(date.get(0));
            String endDate = DateUtils.dealDateFormat(date.get(1));

            list = TDengineMapper.DateEquipmentInfoData(equipmentNumber, startDate, endDate);
            total = TDengineMapper.DateEquipmentInfoTotal(equipmentNumber, startDate, endDate);
        } else {
            list = TDengineMapper.AllEquipmentInfoData(equipmentNumber);
            total = TDengineMapper.AllEquipmentInfoTotal(equipmentNumber);
        }

        Equipment equipment = dataTableMapper.FindAEquipment(equipmentNumber);
        hashMap.put("items", list);
        hashMap.put("total", total);
        hashMap.put("equipmentNumber", equipment.getEquipmentNumber());
        hashMap.put("equipmentName", equipment.getEquipmentName());
        hashMap.put("uploadFrequency", equipment.getUploadFrequency());
        hashMap.put("province", equipment.getProvince());
        hashMap.put("region", equipment.getRegion());
        hashMap.put("site", equipment.getSite());
        hashMap.put("pipeline", equipment.getPipeline());
        hashMap.put("principal", equipment.getPrincipal());

        return Result.ok("true", 200, "成功", hashMap);
    }

    public Result FindOptions() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("equipmentNumberOptions", dataTableMapper.FindEquipmentNumber());
        hashMap.put("regionOptions", personMapper.FindRegionOptions());
        hashMap.put("pipelineOptions", personMapper.FindPipelineOptions());

        return Result.ok("true", 200, "成功", hashMap);
    }
}
